var findSubstring = function (s, words) {
  var newarr = [];
  for (var i = 0; i < words.length; i++) {
    var a = "/" + words[i] + "/i";
    var b = eval(a);
    if (s.match(b) !== -1) {
      newarr.push(s.match(b).index);
    }
  }
  return newarr;
};
var s1 = "barfoothefoobarman";
var b1 = ["foo", "bar"];
console.log(findSubstring(s1, b1));